<?php //tailvh@gmail.com

//require forms file
require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS.DS."Page.php";

//require objects file
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS.DS."Page.php";

//require models file
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Pages.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Users.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Categorys.php";

class Admin_PageController extends Zend_Controller_Action
{
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelPage;
    private $modelUser;
    private $currentUser;
    private $modelCategory;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////    
    public function init()
    {
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {         
            $this->_redirect('admin/auth/login');
        } 
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        //init variable
        //MODEL
        $this->modelPage = new End_Models_Pages();
        $this->modelUser = new End_Models_Users();
        $this->modelCategory = new End_Models_Categorys();
        //OTHER 
        $this->currentUser = $this->getInforUser();
        
        //VIEW 
        $this->view->currentUser = $this->currentUser; 
        /* Initialize action controller here */
    }
    
    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }

    
    ////////////////////
    // A C T I O N S  //
    ////////////////////    
    public function addAction()
    {   
        $arrName = $this->modelCategory->_list();
       
        //Create form enter info for new Page
    	$form = new Application_Form_Page(); 
    	//Set label for the button
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        //Set client view page form  = $form
        $form->parent_id->AddMultiOptions($arrName);
        $this->view->form = $form;
        
        $this->view->bodyCopy = "<p>Nhập các thông tin về trang vào form dưới đây:</p>";
        $created_date = date('Y-m-d H:i:s');
        $user_id=$this->modelUser->_getUserId($this->currentUser);
        //If can get client request 
        if ($this->getRequest()->isPost()) 
        {
            //cancel button     
            $cancel = $this->getRequest()->getPost('cancel', '0');
            
            if($cancel)
            {
                $this->_helper->redirector('index');
            }
            
            //Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData))   //If data is valid 
            {
            	//Get data one by one
                $name = $form->getValue('name');
      //          $slug = $form->getValue('slug');
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $parent_id = (int)$form->getValue('parent_id');
                $num_views     = 0;
		$language= $form->getValue('language');
              
                //Create new  Page object from datas
                $page = new Page(null, $name,'', $description, $content, '1','1' ,$parent_id, '0', $created_date, null,$user_id, $language,0);
         
                //Use insert function from  Page model
                $this->modelPage->_insert($page);
                
                //Move to index page
                $this->_helper->redirector('index');
            } else 
            {
            	//Return form
                $form->populate($formData);
            }
        }
    }
    
    public function deleteAction()
    {	
    	if ($this->getRequest()->isPost()) 
    	{
            $del = $this->getRequest()->getPost('del'); 
    		if ($del == 'Yes') 
            {
                $page_id = (int)$this->getRequest()->getPost('page_id');
                $this->modelPage->_delete($page_id);
            }
            $this->_helper->redirector('index');
        } else 
        {
            $page_id = $this->_getParam('page_id', 0);
            $this->view->Pages = Utility_Converter::objectToArray($this->modelPage->_find($page_id));
        }
    }
    
    public function editAction()
    {
    	$page_id = $this->_getParam('page_id');
    	$is_active = $this->_getParam('is_active');
    	$created_date = $this->_getParam('created_date');
    	$num_view = $this->_getParam('num_view');
    	$user_id = $this->_getParam('user_id');
        $arrName = $this->modelCategory->_list();
        
        //create form
        $form = new Application_Form_Page();
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $this->view->form = $form;
    	$form->parent_id->AddMultiOptions($arrName);
        
        $modified_date= date('Y-m-d H:i:s');
        if ($this->getRequest()->isPost()) 
        {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if($cancel)
            {
               $this->_helper->redirector('index');
            }
            
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {	
               	$name = $form->getValue('name');
      //          $slug = $form->getValue('slug');
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $parent_id = (int)$form->getValue('parent_id');
                $language= $form->getValue('language');
                
                $page = new Page($page_id, $name,'', $description, $content, null,null ,$parent_id,$num_view, $created_date,$modified_date,$user_id, $language,$is_active);							
                $this->modelPage->_update($page);								
                
                $this->_helper->redirector('index');				
            } else 
            {
                $form->populate($formData);
            }
        } else 
        {
            $page_id = $this->_getParam('page_id', 0);
            if ($page_id > 0) 
            {
                $form->populate(Utility_Converter::objectToArray($this->modelPage->_find($page_id)));
            }
        }
    }
    
    public function indexAction()
    {
        $page = $this->modelPage->fetchAll();
        $this->view->Pages = $page;
    }
    
	public function activeAction()
    {
    	if ($this->getRequest()->isPost()) 
        {
            $active = $this->getRequest()->getPost('active');
            if($active == 'Yes')
            {
               $page_id = $this->_getParam('page_id');
        		$page = $this->modelPage->_find($page_id);
       			$this->modelPage->_active($page);
       			
            }
            $this->_helper->redirector('index');
            
    	}
    
	}
}






